package com.collect.senegal.zhiScene.zhiAuditer.zhiPerformance.detail;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiAuditerPerfermance.ZHIAuditerPerfermanceDetail;
import com.collect.senegal.zhiBase.BaseActivity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAuditerWholeKfDayBonusDetailEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISAuditerWholeKfDayBonusDetailEntity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.senegal.zhiScene.zhiAuditer.zhiPerformance.ZHIAuditerPerfermanceManager;
import com.collect.senegal.zhiScene.zhiWidget.zhiAuditer.ZHIAuditerDateSelect;
import com.collect.senegal.zhiScene.zhiWidget.zhiNavigationBar.ZHINavigationBar;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_AUDITER_PERFERMANCE_DETAIL)
public class ZHIAuditerPerfermanceDetailActivity extends BaseActivity<ZHIAuditerPerfermanceDetailPresenter> implements ZHIAuditerPerfermanceDetailContract.View {

    @BindView(R.id.navigationBar)
    ZHINavigationBar navigationBar;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.dateAuditerSelect)
    ZHIAuditerDateSelect dateAuditerSelect;

    @BindView(R.id.editAuditerPerfermanceMobile)
    EditText editAuditerPerfermanceMobile;

    @BindView(R.id.textAuditerPerfermanceRuleAll)
    TextView textAuditerPerfermanceRuleAll;

    @BindView(R.id.textAuditerPerfermanceRuleReward)
    TextView textAuditerPerfermanceRuleReward;

    @BindView(R.id.textAuditerPerfermanceRulePenalty)
    TextView textAuditerPerfermanceRulePenalty;

    @BindView(R.id.textAuditerPerfermanceRuleSysDeny)
    TextView textAuditerPerfermanceRuleSysDeny;

    @BindView(R.id.textAuditerPerfermanceRuleReset)
    TextView textAuditerPerfermanceRuleReset;

    @BindView(R.id.textAuditerPerfermanceQuery)
    TextView textAuditerPerfermanceQuery;

    @BindView(R.id.textAuditerPerfermanceDetailResultCountValue)
    TextView textAuditerPerfermanceDetailResultCountValue;

    @BindView(R.id.rcAuditerPerfermanceDetailList)
    RecyclerView rcAuditerPerfermanceDetailList;

    ZHIAuditerPerfermanceDetail mZHIAuditerPerfermanceDetail = new ZHIAuditerPerfermanceDetail();

    private int mMonth = 0;
    private String mLevel = "ALL";

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_auditer_perfermance_detail;
    }

    @Override
    protected void initView() {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            navigationBar.showLeft().hideRight().setCenterText(getString(R.string.page_perfermance)).setBarClickListener(new ZHINavigationBar.OnNavigationBarClickListener() {
                @Override
                public void onNavigationBarLeftClick(View v) {
                    finish();
                }

                @Override
                public void onNavigationBarRightClick(View v) {
                }
            });

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetWholeKfDayBonusDetail(ZHIRAuditerWholeKfDayBonusDetailEntity zhirAuditerWholeKfDayBonusDetailEntity) {
        srl_swipe_layout.setRefreshing(false);

        if (zhirAuditerWholeKfDayBonusDetailEntity != null) {
            textAuditerPerfermanceDetailResultCountValue.setText(getString(R.string.auditer_bonus_detail_result_count, zhirAuditerWholeKfDayBonusDetailEntity.getResult().size() + ""));

            // 更新列表
            mZHIAuditerPerfermanceDetail.setNewData(zhirAuditerWholeKfDayBonusDetailEntity.getResult());
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        // 更新bar title
        if (ZHIAuditerPerfermanceManager.getInstance().type.equalsIgnoreCase("ALL")) {
            navigationBar.setCenterText("All");
        } else if (ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay.length() > 0) {
            navigationBar.setCenterText(ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay);
            dateAuditerSelect.setStartDate(ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay);
        }

        // 更新状态
        ZHIAuditerPerfermanceManager.getInstance().type = "ALL";
        textAuditerPerfermanceRuleAll.setBackgroundResource(R.drawable.auditer_perfermance_rule_selected);
        textAuditerPerfermanceRuleReward.setBackgroundResource(R.drawable.auditer_perfermance_rule_unselect);
        textAuditerPerfermanceRulePenalty.setBackgroundResource(R.drawable.auditer_perfermance_rule_unselect);
        textAuditerPerfermanceRuleSysDeny.setBackgroundResource(R.drawable.auditer_perfermance_rule_unselect);

        // 设置空布局
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        mZHIAuditerPerfermanceDetail.setEmptyView(emptyView);

        rcAuditerPerfermanceDetailList.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rcAuditerPerfermanceDetailList.setAdapter(mZHIAuditerPerfermanceDetail);

        // 监听手机输入
        editAuditerPerfermanceMobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                ZHIAuditerPerfermanceManager.getInstance().phoneNum = editAuditerPerfermanceMobile.getText().toString().trim();
            }
        });

        // 设置条件查询
        textAuditerPerfermanceRuleAll.setOnClickListener(view -> {
            ZHIAuditerPerfermanceManager.getInstance().type = "ALL";

            setRuleBeSelected(textAuditerPerfermanceRuleAll, true);
            setRuleBeSelected(textAuditerPerfermanceRuleReward, false);
            setRuleBeSelected(textAuditerPerfermanceRulePenalty, false);
            setRuleBeSelected(textAuditerPerfermanceRuleSysDeny, false);
        });

        textAuditerPerfermanceRuleReward.setOnClickListener(view -> {
            ZHIAuditerPerfermanceManager.getInstance().type = "BONUS";

            setRuleBeSelected(textAuditerPerfermanceRuleAll, false);
            setRuleBeSelected(textAuditerPerfermanceRuleReward, true);
            setRuleBeSelected(textAuditerPerfermanceRulePenalty, false);
            setRuleBeSelected(textAuditerPerfermanceRuleSysDeny, false);
        });

        textAuditerPerfermanceRulePenalty.setOnClickListener(view -> {
            ZHIAuditerPerfermanceManager.getInstance().type = "PENALTY";

            setRuleBeSelected(textAuditerPerfermanceRuleAll, false);
            setRuleBeSelected(textAuditerPerfermanceRuleReward, false);
            setRuleBeSelected(textAuditerPerfermanceRulePenalty, true);
            setRuleBeSelected(textAuditerPerfermanceRuleSysDeny, false);
        });

        textAuditerPerfermanceRuleSysDeny.setOnClickListener(view -> {
            ZHIAuditerPerfermanceManager.getInstance().type = "SYS_REFUSE";

            setRuleBeSelected(textAuditerPerfermanceRuleAll, false);
            setRuleBeSelected(textAuditerPerfermanceRuleReward, false);
            setRuleBeSelected(textAuditerPerfermanceRulePenalty, false);
            setRuleBeSelected(textAuditerPerfermanceRuleSysDeny, true);
        });

        textAuditerPerfermanceRuleReset.setOnClickListener(view -> {
            ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay = "";
            ZHIAuditerPerfermanceManager.getInstance().phoneNum = "";
            ZHIAuditerPerfermanceManager.getInstance().type = "ALL";

            editAuditerPerfermanceMobile.setText(null);
            dateAuditerSelect.setStartDate("");

            setRuleBeSelected(textAuditerPerfermanceRuleAll, true);
            setRuleBeSelected(textAuditerPerfermanceRuleReward, false);
            setRuleBeSelected(textAuditerPerfermanceRulePenalty, false);
            setRuleBeSelected(textAuditerPerfermanceRuleSysDeny, false);
        });

        textAuditerPerfermanceQuery.setOnClickListener(view -> {
            initData(true);
        });

        dateAuditerSelect.setStartDate(ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay);
        dateAuditerSelect.setDateItemClick(date -> {
            ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay = dateAuditerSelect.getStartDate();
            initData(true);
        });
    }

    public void setRuleBeSelected(TextView ruleTextView, Boolean selected) {
        if (ruleTextView != null) {
            if (selected) {
                ruleTextView.setBackgroundResource(R.drawable.auditer_perfermance_rule_selected);
                ruleTextView.setTextColor(ContextCompat.getColor(this, R.color.white));
            } else {
                ruleTextView.setBackgroundResource(R.drawable.auditer_perfermance_rule_unselect);
                ruleTextView.setTextColor(ContextCompat.getColor(this, R.color.theme_black_2));
            }
        }
    }

    public void initData(boolean showDialog) {
        assert mPresenter != null;
        ZHISAuditerWholeKfDayBonusDetailEntity zhisAuditerWholeKfDayBonusDetailEntity = new ZHISAuditerWholeKfDayBonusDetailEntity();
        zhisAuditerWholeKfDayBonusDetailEntity.setPhoneNum(ZHIAuditerPerfermanceManager.getInstance().phoneNum);
        zhisAuditerWholeKfDayBonusDetailEntity.setYearAndMonthAndDay(ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay);
        zhisAuditerWholeKfDayBonusDetailEntity.setYearAndMonth(ZHIAuditerPerfermanceManager.getInstance().yearAndMonth);
        zhisAuditerWholeKfDayBonusDetailEntity.setType(ZHIAuditerPerfermanceManager.getInstance().type);
        mPresenter.getWholeKfDayBonusDetail(getActivity(), zhisAuditerWholeKfDayBonusDetailEntity, showDialog);
    }
}